import { uuid } from 'vue-uuid';
import request from "@/axios"
export default ({
    namespaced:true,
    state: {

      //搜索组件状态
      SearchState:false,

      //搜索的状态 -1:表示未搜索 0:输入了值但是删除到了空白 1:加载中 2:正常的搜索结果  3:空数据  4:网络问题或者非法输入等问题
      SearchProcess:-1,

      //搜索历史数据
      SearchHistoryData:JSON.parse(localStorage.getItem('SearchHistoryData'))|| [],

      //搜索结果
      SearchResult:{},

      //搜索导航
      NaviIndex:1,

      //关键字
      keyword:'',

      //搜索结果
      SearchResultDetail:[],
      SearchInfo:{pageNo:1,pageSize:5,total:0,length:0}
     
    },
    getters: {},
    mutations: {

      //开启搜索组件
      SEARCHON:state=>state.SearchState=true,
      //关闭搜索组件
      SEARCHDOWN:state=>state.SearchState=false,
      
      //修改搜索状态
      SETSEARCHPROCESS:(state,key) =>state.SearchProcess=key,

      //修改搜索结果
      SETSEARCHRESULT:(state,res) =>state.SearchResult=res,

      //增加搜索数据
      ADDSEARCHHISTORY:(state,data)=>{
         let newdata={}
         newdata=Object.assign(newdata,data)
         state.SearchHistoryData =  state.SearchHistoryData.filter(item => item.value != data.value)
         state.SearchHistoryData.unshift(newdata)
         if(state.SearchHistoryData.length>=21) //做出数量限制
            state.SearchHistoryData.splice(state.SearchHistoryData.length-1,1)
         localStorage.setItem('SearchHistoryData',JSON.stringify(state.SearchHistoryData))
      },

      //减少搜索数据
      DELETESEARCHHISTORY:(state,id)=>{
        let index=state.SearchHistoryData.findIndex(item => item.id== id)
        state.SearchHistoryData.splice(index,1)
        localStorage.setItem('SearchHistoryData',JSON.stringify(state.SearchHistoryData))
     },
     
     //重置搜索数据
     REFRESSEARCHHHISTORY:(state)=>{
      state.SearchHistoryData=[]
      localStorage.setItem('SearchHistoryData',JSON.stringify(state.SearchHistoryData))
     },
     
     //重置搜索结果和状态
     REFRESH:(state)=>{
       state.SearchResult={}
       state.SearchProcess=-1
     },
     //
     REFRESHDETAIL:(state)=>{
        state.NaviIndex = 1
        state.SearchResultDetail = []
        state.SearchInfo = {pageNo:1,pageSize:5,total:0,length:0}
        state.keyword = ''
     },
     //设置导航
     SETNAVIINDEX:(state,index) => {
        state.NaviIndex = index
        state.SearchResultDetail = []
        state.SearchInfo = {pageNo:1,pageSize:5,total:0,length:0}
     },
     //设置分页
     SETSEARCHINFO(state,data){
      state.SearchInfo = { ...state.SearchInfo,...data}
     },
       
     //设置搜索关键字
      SETKEYWORD:(state,keyword) => state.keyword =keyword,

      //设置搜索的详情
      SETSEARCHRESULTDETAIL:(state,{records,total}) =>{
        state.SearchResultDetail = records || []
        state.SearchInfo.total = total
        state.SearchInfo.length =  state.SearchResultDetail.length
      }
    },
    actions:{
      search(context,keyword){
        let id=uuid.v1() 
        let data={id,value:keyword}
        context.commit('ADDSEARCHHISTORY',data)
      },
     async  getSearchResult(context,keyword){  
          try{
            let res = await request.get('/api/search/all',{params:{keyword}})
            if(res.data.code==200)
            {
               context.commit('SETSEARCHRESULT',res.data.data)
               if(res.data.data.total==0) //空数据
               context.commit('SETSEARCHPROCESS',3)
               else if(context.state.SearchProcess==0) //此时搜索框的内容为空，应该将状态置为-1
               context.commit('SETSEARCHPROCESS',-1)
               else context.commit('SETSEARCHPROCESS',2) //正常返回结果
            }
          }catch(error){context.commit('SETSEARCHPROCESS',4)}//异常处理
         
      },

      async getSearchResultDetail(context,data){
       
        context.commit('SETSEARCHINFO',data)
        let {pageNo,pageSize,keyword,api,order} = context.state.SearchInfo
        context.commit('SETSEARCHRESULTDETAIL',{records:null,total:0})
        try{
          let res =  await request.get(api,{params:{pageNo,pageSize,keyword,order}})
   
          if(res.data.code == 200 && res.data.data != null) context.commit('SETSEARCHRESULTDETAIL',res.data.data) 
          else context.commit('SETSEARCHRESULTDETAIL',{records:null,total:0}) 
          
          return res.data
        }catch(error){return{code:400,message:error}}
      }
    }
})

